# --------------------------------------------------------------------------------------------------------------------
# Project:             Digital Payment System (DPS)
# Website:             http://code.google.com/p/dps-x509/
# Purpose of document: This file contains roulette specific functionality
# --------------------------------------------------------------------------------------------------------------------
import random

import dps.utils

# --------------------------------------------------------------------------------------------------------------------
class RouletteSpace:
  """ Item in roulette that can be bet on """
# --------------------------------------------------------------------------------------------------------------------
  def __init__(self, validValues, factor, displayName, variableName, colspan=1, rowspan=1):
    """ constructor:
    @param validValues - list of valid values that will be considered a win
    @param displayName - label displayed for the space
    @param variableName - unique name used for storing user bets 
    @param colSpan - span in html table 
    @param rowSpan - span in html table 
    """
    self.displayName_ = displayName
    self.variableName_ = variableName
    self.factor_ = factor
    self.validValues_ = validValues
    self.colspan_ = colspan
    self.rowspan_ = rowspan
        
# --------------------------------------------------------------------------------------------------------------------
  def getPayout(self, spin):
    """ get the payout for the spin. 0 if no win. """
    ret = 0
    if self.isWin(spin):
      ret = self.factor_
    return ret
  
# --------------------------------------------------------------------------------------------------------------------
  def isWin(self, spin):
    """ is the spin value a winner """
    return spin in self.validValues_
    
# --------------------------------------------------------------------------------------------------------------------
  def getHtmlCell(self, value, lastSpin):
    """ return a td for the space """
    rowspan = ""
    if self.rowspan_ > 1:
      rowspan = " rowspan='%d'" % self.rowspan_
    colspan = ""
    if self.colspan_ > 1:
      colspan = " colspan='%d'" % self.colspan_
    color = ""
    if self.isWin(lastSpin):
      color = " bgcolor='#CBFCBF'"
    value = str(value)
    if value == "0":
      value = ""
    return "<td%s%s%s>%s: <input type='int' style='width:40px;' name='%s' value='%s'/></td>" % \
    (rowspan, colspan, color, self.displayName_, self.variableName_, value)

# --------------------------------------------------------------------------------------------------------------------
class RouletteBoard:
  """ Container for the roulette spaces """
  
  def __init__(self):
    """ create the components used to make up the roulette board """
    self.rows_ = []
    self.spaces_ = {}
    for i in range(0, 5):
      self.rows_.append([])

    self.__addSpace(0, RouletteSpace([0], 35, "0", "rs0", 1, 5))
    #add individual numbers
    for i in range(1, 37):
      self.__addSpace((i-1) / 12, RouletteSpace([i], 36, str(i), "rs"+str(i)))
      
    #add thirds
    self.__addSpace(3, RouletteSpace(range(1, 13), 2, "1 - 12", "rs1To12", 4))
    self.__addSpace(3, RouletteSpace(range(13, 25), 2, "13 - 24", "rs13To24", 4))
    self.__addSpace(3, RouletteSpace(range(25, 37), 2, "25 - 36", "rs25To36", 4))

    #add red and black
    redlist = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
    self.__addSpace(4, RouletteSpace(redlist, 2, "Red", "rsRed", 6))
    blacklist = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
    self.__addSpace(4, RouletteSpace(blacklist, 2, "Black", "rsBlack", 6))
  
# --------------------------------------------------------------------------------------------------------------------
  def __addSpace(self, row, rs):
    """ add space to board """
    self.rows_[row].append(rs)
    self.spaces_[rs.variableName_] = rs
    
# --------------------------------------------------------------------------------------------------------------------
  def getUserInputs(self, request):
    """ convert post request to map """
    values = {}
    for key in self.spaces_:
      values[key] = dps.utils.toInt(request.get(key))
    return values
    
# --------------------------------------------------------------------------------------------------------------------
  def getHtmlTable(self, values=None, lastSpin=None):
    """ returns a formatted html table that display wins and user inputs """
    table = "<table border='1'>\n"
    dummyValue = None
    if values == None:
      dummyValue = "0"
    for i in self.rows_:
      table += "<tr>"
      for cell in i:
        table += cell.getHtmlCell(dummyValue or values[cell.variableName_], lastSpin)
      table += "</tr>\n"
    table += "</table>\n"
    
    return table

# --------------------------------------------------------------------------------------------------------------------
  def calculatePayout(self, spin, values):
    """ calculates the winning based on the spin
    @params spin """
    payout = 0
    
    for key in self.spaces_:
      space = self.spaces_[key]
      if values.has_key(space.variableName_):
        payout +=  space.getPayout(spin) * values[key]
    
    return payout      
   
# --------------------------------------------------------------------------------------------------------------------
def calculateBet(values):
  """ calculates the bet based on the values
  @param values is a map of key value pairs where key is the name of the bet and value is the amount bet
  @returns the sum of all the bets """
  bet = 0
  for v in values:
    if values[v] > 0:
      bet += values[v]
      
  return bet  
 
# --------------------------------------------------------------------------------------------------------------------
def getSpin():
  """ 'spins' the wheel and returns the value """
  spin = random.randint(0, 36)
  return spin
  
